export default {
	data() {
		return {
			pay: 2,
			addrInfo: {},
			goodsList: [],
			orders: '',
			allPrice: 0,
			num: 0,
			comment: ''
		};
	},
	onLoad(options) {
		this.goodsList = JSON.parse(decodeURIComponent(options.goodsList))

		console.log('goods', this.goodsList)
		this.orders = JSON.parse(decodeURIComponent(options.orders))
		this.getAllprice()
		this.getAddrList()

		console.log('gooslist===', this.goodsList)

		uni.$on('chooseAddr', res => {
			this.addrInfo = res.addr
		});
	},
	methods: {
		getAllprice() {
			this.allPrice = 0
			this.num = 0
			this.goodsList.forEach((item) => {
				this.allPrice += Number(item.buymoney) * Number(item.num)
				this.num += Number(item.num)
			})
		},
		// 获取地址列表
		async getAddrList() {
			var res = await this.$get('server/index/addressList');
			if (res.errcode != 0) {
				this.$toast(res.errmsg);
				return;
			}
			if (res.list.length == 0) {
				this.addrInfo = null
			} else {
				this.addrInfo = res.list[0]
			}
		},
		// 确认发货
		async showPay() {
			if (this.addrInfo && this.addrInfo.id) {
				var res = await this.$post('server/index/sendgoods', {
					fahuolist: JSON.stringify(this.orders),
					aid: this.addrInfo.id,
					beizhu: this.comment
				});
				if (res.errcode == 10027) {
					setTimeout(() => {
						this.$navTo('pages/manghe/recharge/recharge')
					}, 700)
				}
				if (res.errcode != 0) {
					this.$toast(res.errmsg);
					return;
				}

				this.$toast('操作成功')
				uni.$emit('uploadStore')
				setTimeout(() => {
					this.$redirectTo('pages/mine/order/order')
				}, 500)
			} else {
				this.$toast('请添加地址')
			}
		},
		// 新增收货地址
		addAddr() {
			console.log('点击添加地址')
			this.$navTo('pages/mine/address-edit/address-edit', {
				isOrder: 1
			})
		},
		// 选择收货地址
		chooseAddr() {
			this.$navTo('pages/mine/address/address', {
				choose: 1
			})
		},
	}
}
